﻿using System;
using System.Collections.Generic;
using System.Formats.Tar;
using System.IO;
using System.Linq;
using System.Net;
using AppData;
using static System.Net.Mime.MediaTypeNames;

public class VideosSaver
{
    public static string[] AllFileExt = { ".mp4", ".avi"};

    public static int SaveVideo(string genre, long movieId, int episodeId,byte[] contentData)
    {
        return SaveVideo(ServerConfig.videosRootDir, genre, movieId, episodeId,contentData);
    }
    public static byte[] LoadVideo(long movieId, int episodeId, string genre)
    {
        return LoadVideo(ServerConfig.videosRootDir, movieId, episodeId, genre,"");
    }
  
    public static void DeleteVideo(string videosRootDir, long movieId, int episodeId, string genre)
    {
        DeleteVideo(GetVideoDir(videosRootDir,genre), movieId, episodeId);
    }
    public static void DeleteVideo(string videoDir, long movieId,int episodeId)
    {
        foreach (var ext in AllFileExt)
        {
            string texFilePath = GetFilePath(videoDir, movieId, episodeId, ext);
            if (File.Exists(texFilePath))
                File.Delete(texFilePath);
        }
    }
    public static int SaveVideo(string videosRootDir, string genre, long movieId, int episodeId,byte[] contentData)
    {
        try
        {
            if (contentData == null || contentData.Length <= 0)
            {
                GameDebug.LogError("SaveVideo error");
                return -1;
            }
            string videoDir = videosRootDir + "/" + genre.ToString();
            if (!Directory.Exists(videoDir))
            {
                GameDebug.Log($"create Video dir:{videoDir}");
                Directory.CreateDirectory(videoDir);
            }
            //string texString = Encoding.UTF8.GetString(tex);
            string filePath = GetFilePath(videoDir, movieId, episodeId, ".mp4");
            //string infoFilePath = GetInfoFilePath(bookDir, book.id.ToString());

            File.WriteAllBytes(filePath, contentData);
            //File.WriteAllText(infoFilePath, info);
            return 0;
        }
        catch (Exception ex)
        {
            GameDebug.LogException(ex);
            return -1;
        }
    }

    public static string GetVideoDir(string videosRootDir, string genre) => videosRootDir + "/" + genre.ToString();

    static byte[] TryGetVideoFile(string videoDir,long movieId,int episodeId, string fileExt)
    {
        if (!string.IsNullOrEmpty(fileExt))
        {
            string texFile = GetFilePath(videoDir, movieId,episodeId, fileExt);
            if (File.Exists(texFile))
                return File.ReadAllBytes(texFile);
        }
        foreach (var ext in AllFileExt)
        {
            string videoFile = GetFilePath(videoDir, movieId, episodeId, ext);
            if (File.Exists(videoFile))
                return File.ReadAllBytes(videoFile);
        }
        return null;
    }

    public static byte[] LoadVideo(string videosRootDir, long movieId, int episodeId,string genre,string ext)
    {
        string videoDir = GetVideoDir(videosRootDir , genre);

        var file = TryGetVideoFile(videoDir, movieId, episodeId, ext);
        return file;
    }
    public static string GetFilePath(string dir, long videoId, int episodeId, string ext)
    {
        string fileName = GetFileName(videoId , episodeId, ext);
        string filePath = dir + "/" + fileName;
        return filePath;
    }

    public static string GetFileName(long movieId, int episodeId, string ext) => movieId + "_" + episodeId + ext;

    public static string GetUrl(string fileDir, string relativePath, long movieID,int episodeId,string ext)
    {
        string fileName = GetFileName(movieID , episodeId, ext);

#if !RELEASE
        string url = $"https://{ServerConfig.testDomain}:{ServerConfig.port}/Movies/get_video?relativePath={relativePath}/{fileName}";
#else 
        string url = $"https://{ServerConfig.domain}:{ServerConfig.port}/Movies/get_video?relativePath={relativePath}/{fileName}";
#endif

        //GameDebug.Log($"GetTextureUrl:{url}");
        return url;
    }

    public static string GetVideoUrl(long movieId, int episodeId, string genre,bool nullOnNoTex = true)
    {
        string videoDir = GetVideoDir(ServerConfig.videosRootDir, genre);
        string fileExt = "";
        if (nullOnNoTex)
        {
            string videoFile = GetFilePath(videoDir, movieId, episodeId, ".mp4");
            if (File.Exists(videoFile))
                fileExt = ".mp4";
            else
            {
                foreach (var ext in AllFileExt)
                {
                    string videoFile2 = GetFilePath(videoDir, movieId, episodeId, ext);
                    if (File.Exists(videoFile))
                    {
                        fileExt = ext;
                        break;
                    }
                }
            }
            if (string.IsNullOrEmpty(fileExt))
            {
                return "";
            }
        }
        string relativeDir = ServerConfig.videoRelativePath + "/" + genre;
        return GetUrl(videoDir, relativeDir, movieId, episodeId, fileExt);
    }

}

